package com.mxnet.chongjin.supoman.activity;

import android.app.Instrumentation;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.mxnet.chongjin.supoman.R;

import java.util.ArrayList;
import java.util.List;

public class ChooseMachineActivity extends AppCompatActivity implements View.OnClickListener {
    private Context mContext;
    private ListView mListView;
    private LvNearMachineAdapter mLvNearMachineAdapter;
    private List mList;
    private LinearLayout mLinearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_machine);
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        mContext = ChooseMachineActivity.this;

        initData();
        initView();
    }

    public void initView() {
        mListView = findViewById(R.id.lv_near_machine);
        mLvNearMachineAdapter = new LvNearMachineAdapter(mContext, mList);
        mListView.setAdapter(mLvNearMachineAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.setClass(mContext, NetConfigActivity.class);
                //                mContext.startActivity(intent);
            }
        });

        mLinearLayout = findViewById(R.id.ll_add_back);
        mLinearLayout.setOnClickListener(this);
    }

    public void initData() {
        mList = new ArrayList();
        mList.add("1");
        mList.add("2");
        mList.add("3");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_add_back:
                new Thread() {
                    public void run() {
                        try {
                            Instrumentation inst = new Instrumentation();
                            inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
                break;
        }
    }

    public class LvNearMachineAdapter extends BaseAdapter {
        public List mList;
        public Context mContext;

        public LvNearMachineAdapter(Context context, List list) {
            super();
            this.mContext = context;
            this.mList = list;
        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_lv_near_machine, parent, false);
            return convertView;
        }
    }
}
